package cn.com.anysdk.demo.controller;

import cn.com.anysdk.oss.api.IOssService;
import cn.com.anysdk.oss.api.UploadOptions;
import cn.com.anysdk.oss.exception.OssException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/minio")
public class MinioOssController {

    @Autowired
    @Qualifier("minioOssService")
    private IOssService ossService;

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 创建临时文件
            Path tempFile = Files.createTempFile("upload-", file.getOriginalFilename());
            file.transferTo(tempFile.toFile());

            // 上传到 MinIO
            String objectKey = "uploads/" + file.getOriginalFilename();
            String url = ossService.uploadFile(objectKey, tempFile.toFile());

            // 删除临时文件
            Files.delete(tempFile);

            Map<String, String> result = new HashMap<>();
            result.put("url", url);
            result.put("objectKey", objectKey);
            return result;
        } catch (IOException e) {
            throw new OssException("Failed to upload file: " + e.getMessage(), e);
        }
    }

    @GetMapping("/download/{objectKey}")
    public void downloadFile(@PathVariable String objectKey) {
        try (InputStream inputStream = ossService.downloadFile(objectKey)) {
            // 创建临时文件
            Path tempFile = Files.createTempFile("download-", objectKey);
            try (FileOutputStream outputStream = new FileOutputStream(tempFile.toFile())) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }
            // 这里可以添加文件下载的逻辑
            Files.delete(tempFile);
        } catch (IOException e) {
            throw new OssException("Failed to download file: " + e.getMessage(), e);
        }
    }

    @DeleteMapping("/{objectKey}")
    public void deleteFile(@PathVariable String objectKey) {
        ossService.deleteFile(objectKey);
    }

    @GetMapping("/url/{objectKey}")
    public Map<String, String> getFileUrl(@PathVariable String objectKey) {
        String url = ossService.getFileUrl(objectKey);
        Map<String, String> result = new HashMap<>();
        result.put("url", url);
        return result;
    }

    @GetMapping("/exists/{objectKey}")
    public Map<String, Boolean> doesObjectExist(@PathVariable String objectKey) {
        boolean exists = ossService.doesObjectExist(objectKey);
        Map<String, Boolean> result = new HashMap<>();
        result.put("exists", exists);
        return result;
    }
}